﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PressureTest
{
    public partial class AddTemplateForm : Form
    {

        /// <summary>模型
        /// 
        /// </summary>
        public Template obj = new Template();

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = NumberTextBox.Text;
            
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            int count = list.Count;
            if (count > 0)
            {
                Alert.Error(where["number"] + "模板编号已存在");
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>更新obj
        /// 
        /// </summary>
        /// <returns></returns>
        private Template UpdateObj()
        {
            obj.number = NumberTextBox.Text.ToInt();
            obj.pipe_position = PipePositionComboBox.Text;
            obj.design_pressure = DesignPressureTextBox.Text.ToFloat();
            if (EditEnableComboBox.Text == "是")
            {
                obj.edit_enable = 1;
            }
            else
            {
                obj.edit_enable = 0;
            }
            obj.first_stage_pressure = FirstStagePressureTextBox.Text.ToFloat();
            obj.first_stage_minute = FirstStageMinuteTextBox.Text.ToInt();
            obj.first_stage_estimate = FormatEstimate(FirstStageEstimateComboBox.Text);
            obj.first_stage_estimate_value = FirstStageEstimateValueTextBox.Text.ToFloat();

            obj.second_stage_pressure = SecondStagePressureTextBox.Text.ToFloat();
            obj.second_stage_minute = SecondStageMinuteTextBox.Text.ToInt();
            obj.second_stage_estimate = FormatEstimate(SecondStageEstimateComboBox.Text);
            obj.second_stage_estimate_value = SecondStageEstimateValueTextBox.Text.ToFloat();

            obj.third_stage_pressure = ThirdStagePressureTextBox.Text.ToFloat();
            obj.third_stage_minute = ThirdStageMinuteTextBox.Text.ToInt();
            obj.third_stage_estimate = FormatEstimate(ThirdStageEstimateComboBox.Text);
            obj.third_stage_estimate_value = ThirdStageEstimateValueTextBox.Text.ToFloat();

            obj.rigor_stage_pressure = RigorStagePressureTextBox.Text.ToFloat();
            obj.rigor_stage_minute = RigorStageMinuteTextBox.Text.ToInt();
            obj.rigor_stage_estimate = FormatEstimate(RigorStageEstimateComboBox.Text);
            obj.rigor_stage_estimate_value = RigorStageEstimateValueTextBox.Text.ToFloat();

            obj.intensity_stage_pressure = IntensityStagePressureTextBox.Text.ToFloat();
            obj.intensity_stage_minute = IntensityStageMinuteTextBox.Text.ToInt();
            obj.intensity_stage_estimate = FormatEstimate(IntensityStageEstimateComboBox.Text);
            obj.intensity_stage_estimate_value = IntensityStageEstimateValueTextBox.Text.ToFloat();
            return obj;
        }

        /// <summary>操作成功
        /// 
        /// </summary>
        private void OperationSuccess()
        {
            ManageTemplateForm frm = (ManageTemplateForm)this.Owner;
            frm.Fresh();
            this.Close();
        }

        public AddTemplateForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void AddTemplateForm_Load(object sender, EventArgs e)
        {
           
        }

        /// <summary>确定
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (obj.number > 0)
            {
                bool repeatRes = CheckRepeat();
                if (repeatRes)
                {
                    return;
                }
                UpdateObj();
                List<Template> list = new List<Template>();
                list.Add(obj);
                int res = SqlSugarHelper.Updateable<Template>(list);
                if (res > 0)
                {
                    OperationSuccess();
                }
                else
                {
                    Alert.Error("编辑点位失败");
                }
            }
            else
            {
                bool repeatRes = CheckRepeat();
                if (repeatRes)
                {
                    return;
                }
                UpdateObj();
                List<Template> list = new List<Template>();
                list.Add(obj);
                int res = SqlSugarHelper.Insertable(list);
                if (res > 0)
                {
                    OperationSuccess();
                }
                else
                {
                    Alert.Error("添加点位失败");
                }
            }
        }

        /// <summary>格式化判断方式
        /// 
        /// </summary>
        /// <param name="estimate"></param>
        /// <returns></returns>
        private int FormatEstimate(string str)
        {
            if (str == "固定数值")
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }
}
